﻿/*----------------------------------------------------------------
// PiPiBugNet
// Copyright (C) 2009-2010 
//
// 功能描述：用户数据访问类
//
// 创建人:张沛沛
// 创建日期: 2009.08.12
// 
// 修改标识:
// 修改描述:
//
//----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

using DBUtility;
using Model;

namespace SQLServerDAL
{
    public class UserDAL
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        static string ConnStr = ConfigurationManager.AppSettings["ConnectionString"];

        /// <summary>
        /// 添加一个用户
        /// </summary>
        /// <param name="user"></param>
        public static void AddUser(User user)
        {
            SqlHelper.ExecuteNonQuery(ConnStr, "Pr_UserAddUser", user.UserName, user.PassWord, user.RoleID);
        }

        /// <summary>
        /// 用户登录时检验用户
        /// </summary>
        /// <param name="sName"></param>
        /// <param name="sPass"></param>
        /// <returns></returns>
        public static User CheckUserLogin(string sName, string sPass)
        {
            User us = new User();
            DataSet ds = SqlHelper.ExecuteDataset(ConnStr, "Pr_UserCheckUserLogin", sName, sPass);
            if (ds.Tables[0].Rows.Count > 0)
            {
                us.ID = Convert.ToInt32(ds.Tables[0].Rows[0]["UserID"]);
                us.UserName = Convert.ToString(ds.Tables[0].Rows[0]["UserName"]);
                us.RoleID = Convert.ToInt32(ds.Tables[0].Rows[0]["RoleID"]);
            }
            return us;
        }

        /// <summary>
        /// 根据用户ID获得用户名称
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <returns>用户名</returns>
        public static string GetUserNameByID(int uid)
        {
            string sName = "";
            object oName = SqlHelper.ExecuteScalar(ConnStr, "Pr_UserGetUserNameByID", uid);
            if (oName != null)
            {
                sName = oName.ToString();
            }
            else
            {
                sName = "未知";
            }

            return sName;
        }

        /// <summary>
        /// 获取用户角色名称列表
        /// </summary>
        /// <returns></returns>
        public static DataSet GetRoleNameList()
        {
            DataSet ds = new DataSet();

            ds = SqlHelper.ExecuteDataset(ConnStr, "Pr_UserGetUserRoleNameList");

            if (ds.Tables[0].Rows.Count < 1)
            {
                ds = null;
            }
            return ds;
        }

        /// <summary>
        /// 获取用户的密码
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <returns>加密后的密码</returns>
        public static string GetUserPassWord(int uid)
        {
            string sPwd = "";
            object oPwd = SqlHelper.ExecuteScalar(ConnStr, "Pr_UserGetPassWord", uid);
            if (oPwd != null)
            {
                sPwd = oPwd.ToString();
            }
            return sPwd;
        }

        /// <summary>
        /// 更新用户的密码
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="pwd">加密后的密码</param>
        public static void ChangeUserPassWord(int uid, string pwd)
        {
            SqlHelper.ExecuteNonQuery(ConnStr, "Pr_UserChangePassWord", uid, pwd);
        }
    }
}
